package com.sweet.imserver.model;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.sweet.imserver.exception.SwRuntimeException;
import io.vertx.core.json.JsonObject;

public class Member {
  private final Long id;
  private final Integer gender; // 0 男 1 女 2 未知
  private final String name; // 昵称
  private final String phone; // 手机号
  private final String password; // 密码

  public static Member fromJson(JsonObject jsonObject) {
    if (jsonObject == null) {
      throw new SwRuntimeException("解析错误");
    }
    String name = jsonObject.getString("name");
    String phone = jsonObject.getString("phone");
    String pwd = jsonObject.getString("password");
    Integer gender = jsonObject.getInteger("gender");
    return new Member(gender, name, phone, pwd);
  }
  public static Member fromJsonDb(String jsonObject) {
    return fromJsonDb(new JsonObject(jsonObject));
  }
  public static Member fromJsonDb(JsonObject jsonObject) {
    if (jsonObject == null) {
      throw new SwRuntimeException("解析错误");
    }
    Long id = jsonObject.getLong("id");
    String name = jsonObject.getString("name");
    String phone = jsonObject.getString("phone");
    String pwd = jsonObject.getString("password");
    Integer gender = jsonObject.getInteger("gender");
    return new Member(id, gender, name, phone, pwd);
  }

  public Member checkRegisterMember() {
    if(gender == null) {
      throw new SwRuntimeException("缺少性别");
    }
    if (gender < 0 || gender > 2) {
      throw new SwRuntimeException("性别不合法");
    }
    if (StrUtil.isBlank(name)) {
      throw new SwRuntimeException("昵称不能为空");
    }
    if (StrUtil.isBlank(phone)) {
      throw new SwRuntimeException("手机号不能为空");
    }
    if (!PhoneUtil.isMobile(phone)) {
      throw new SwRuntimeException("请填写正确的手机号");
    }
    if (StrUtil.isBlank(password)) {
      throw new SwRuntimeException("密码不能为空");
    }
    return this;
  }

  public JsonObject toJson() {
    return new JsonObject().put("id", id + "")
      .put("gender", gender)
      .put("name", name)
      .put("phone", phone)
      .put("password", password);
  }

  public JsonObject toUserNameAndPwd() {
    return new JsonObject().put("id", id)
      .put("pwd", password);
  }

  public Member(Long id, Integer gender, String name, String phone, String password) {
    this.id = id;
    this.gender = gender;
    this.name = name;
    this.phone = phone;
    this.password = password;
  }

  public Member(Integer gender, String name, String phone, String password) {
    this.id = IdUtil.getSnowflakeNextId();
    this.gender = gender;
    this.name = name;
    this.phone = phone;
    this.password = password;
  }

  public Long getId() {
    return id;
  }

  public Integer getGender() {
    return gender;
  }

  public String getName() {
    return name;
  }

  public String getPhone() {
    return phone;
  }

  public String getPassword() {
    return password;
  }

  @Override
  public String toString() {
    return "Member{" +
      "id='" + id + '\'' +
      ", gender=" + gender +
      ", name='" + name + '\'' +
      ", phone='" + phone + '\'' +
      ", password='" + password + '\'' +
      '}';
  }
}
